package com.linked.basemanage.permissioncontrol.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.linked.basemanage.config.RedisKeysConfig;
import com.linked.basemanage.permissioncontrol.bean.po.RoleInfoPO;
import com.linked.basemanage.permissioncontrol.bean.po.SystemInfoPO;
import com.linked.basemanage.permissioncontrol.service.ISystemInfoService;
import com.linked.basemanage.util.HandleRedisUtils;
import com.linked.universal.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author :dbq
 * @date : 2023/2/22 14:51
 * @description : desc
 */
@Slf4j
@RestController
@RequestMapping("systeminfo")
public class SystemInfoController {

    private final ObjectMapper mapper;

    private final ISystemInfoService systemInfoService;

    private final HandleRedisUtils handleRedisUtils;

    @Autowired
    public SystemInfoController(ObjectMapper mapper, ISystemInfoService systemInfoService, HandleRedisUtils handleRedisUtils) {
        this.mapper = mapper;
        this.systemInfoService = systemInfoService;
        this.handleRedisUtils = handleRedisUtils;
    }

    @PostMapping("addSystem")
    Result addSystem(@RequestBody SystemInfoPO param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("新增系统接口 入参：{}", mapper.writeValueAsString(param));
        }
        boolean ret = false;
        try {
            ret = systemInfoService.addSystem(param);
        } catch (Exception e) {
            log.error("新增系统接口 异常！", e);
            return Result.error("系统异常，请联系管理员！");
        }
        return ret ? Result.success() : Result.ok(false, "保存失败！");
    }

    @PostMapping("querySystemInfoList")
    Result querySystemInfoList(@RequestBody SystemInfoPO param) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("查询系统信息接口 入参：{}", mapper.writeValueAsString(param));
        }
        List<SystemInfoPO> sysList = null;
        try {
            sysList = systemInfoService.querySystemInfoList(param);
        } catch (Exception e) {
            log.error("查询系统信息接口 异常", e);
            return Result.error("系统异常，请联系管理员！");
        }
        return CollectionUtils.isEmpty(sysList) ? Result.ok(false, "未查询到数据") : Result.success(sysList);
    }

    @PostMapping("queryAllSystemInfoList")
    Result queryAllSystemInfoList() throws Exception {
        List<SystemInfoPO> sysList = handleRedisUtils.getRedisValueList(RedisKeysConfig.KEY_ALLSYSTEMINFO, systemInfoService -> {
            try {
                return this.systemInfoService.querySystemInfoList(new SystemInfoPO());
            } catch (Exception e) {
                log.error("查询系统信息接口 异常", e);
            }
            return null;
        }, null);
        return CollectionUtils.isEmpty(sysList) ? Result.ok(false, "未查询到数据") : Result.success(sysList);
    }
}
